/**
 * creation: 7-mrt-2008
 */
package org.jfonia.connect;

/**
 * @author wijnand.schepens@gmail.com
 *
 */
public class ArrayElement<T> extends BasicNode implements MutableValue<T>
{
	protected T[] array; 
	protected Value<Integer> index;
	
	/**
	 * Constructs an array-element value. The value is the element of 'array' at 'index'.
	 * 
	 * By default 'this' is a listener of 'index', i.e. a change in 'index' causes this.call().
	 * The same is true after set().
	 * Note that this is not true for the array! A change in the array (not via 'this'), then 
	 * listeners are not informed.    
	 * 
	 * @param array
	 * @param index
	 */
	public ArrayElement(T[] array, Value<Integer> index)
	{
		super();
		this.array = array;
		this.index = index;
		
		index.addCallTarget(this);
	}

	public void set(T newValue)
	{
		array[index.get()] = newValue;
		call(this);
	}
	
	public T get()
	{
		return array[index.get()];
	}

}
